﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExpenseTracker.Entity;
using ExpenseTracker.Controller;

namespace ExpenseTracker
{
    public partial class CategoryList : BaseForm
    {
        private ExpenseTracker.Controller.CategoryController _categoryController;

        public CategoryList(ExpenseTracker.Entity.Account account) : base(account)
        {
            _categoryController = new CategoryController(account);
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            SetupCategoryList();
            PopulateCategoryList();
            base.OnLoad(e);
        }

        protected void SetupCategoryList()
        {
            dgvCategory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvCategory.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            dgvCategory.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            dgvCategory.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dgvCategory.GridColor = Color.Black;
            dgvCategory.RowHeadersVisible = false;

            dgvCategory.ColumnCount = 1;
            dgvCategory.Columns[0].Name = "Title";

            dgvCategory.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvCategory.MultiSelect = false;
            dgvCategory.Dock = DockStyle.Fill;

            dgvCategory.CellDoubleClick += new DataGridViewCellEventHandler(dgvCategory_CellDoubleClick);
        }

        public void PopulateCategoryList()
        {
            dgvCategory.Rows.Clear();
            List<Category> list = _categoryController.GetCategories(TransactionType.Expense);
            int idx = 0;
            foreach (Category item in list)
            {
                string[] row = { item.Title };
                dgvCategory.Rows.Add(row);
                dgvCategory.Rows[idx++].Tag = item.CategoryID;
            }
        }

        protected void dgvCategory_CellDoubleClick(object sender, EventArgs e)
        {
            if (dgvCategory.CurrentRow != null)
            {
                int categoryID = -1;
                if (int.TryParse(dgvCategory.CurrentRow.Tag.ToString(), out categoryID))
                {
                    CategoryForm childForm = new CategoryForm(Account);
                    childForm.Name = Guid.NewGuid().ToString();
                    childForm.MdiParent = this.MdiParent;
                    childForm.Text = "Update Category";
                    childForm.FormAction = FormAction.Update;
                    childForm.Category.CategoryID = categoryID;
                    childForm.OnCategoryFormSaved += new CategoryForm.CategoryFormSaved(PopulateCategoryList);
                    childForm.Show();
                }
            }
        }
    }
}
